﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace Diagram.DynamicDiagram
{
    public class ColumnScale
    {
        #region 构造函数
        public ColumnScale(DataModel model)
        {
            initializeData(model);
            initializeGraphics();
            initializeContextMenu();
        }
        #endregion

        #region Properties
        // 图形元素
        //private RowDefinition _headerDefinition;
        //private RowDefinition _bodyDefinition;
        private ContextMenu _contextMenu;
        private Canvas _headerCanvas;
        private Canvas _scaleCanvas;

        public Canvas HeaderCanvas
        {
            get { return _headerCanvas; }
            set { _headerCanvas = value; }
        }

        public Canvas ScaleCanvas
        {
            get { return _scaleCanvas; }
            set { _scaleCanvas = value; }
        }

        // 数据元素
        private DataModel _model;
        #endregion

        #region 静态图表
        #region 公有函数
        #endregion

        #region 私有函数
        private void initializeData(DataModel model)
        {
            _model = model;
        }

        private void initializeGraphics()
        { 
            // 设置表头
            //_headerDefinition = new RowDefinition() { Height = new GridLength(_model.DefaultHeaderHeight) };
            _headerCanvas = new Canvas() { Height = _model.DefaultHeaderHeight, Width = _model.DefaultScaleWidth };
            _scaleCanvas = new Canvas() { Height = _model.DefaultColumnHeight, Width = _model.DefaultScaleWidth };
            drawStaticBorder();
            drawStaticScale();
        }

        private void initializeContextMenu()
        {
            _contextMenu = new ContextMenu();
            MenuItem add = new MenuItem() { Header = "在右添加一列" };
            add.Click += new RoutedEventHandler(addColumnToTheRight);
            MenuItem alter = new MenuItem() { Header = "更改刻度.." };
            alter.Click += new RoutedEventHandler(alterColumn);
            MenuItem save = new MenuItem() { Header = "保存配置" };
            save.Click += new RoutedEventHandler(saveConfig);

            _contextMenu.Items.Add(add);
            _contextMenu.Items.Add(alter);
            _contextMenu.Items.Add(save);
            _headerCanvas.ContextMenu = _contextMenu;
        }

        private void drawStaticBorder()
        {
            // 绘制边框线
            Brush lineBrush = Brushes.Black;
            double LineThickness = 1;

            // 表头
            Line headerTop = new Line() { X1 = 0, Y1 = 0, X2 = _headerCanvas.Width, Y2 = 0, Stroke = lineBrush, StrokeThickness = LineThickness };
            Line headerLeft = new Line() { X1 = 0, Y1 = 0, X2 = 0, Y2 = _headerCanvas.Height, Stroke = lineBrush, StrokeThickness = LineThickness };
            Line headerRight = new Line() { X1 = _headerCanvas.Width, Y1 = 0, X2 = _headerCanvas.Width, Y2 = _headerCanvas.Height, Stroke = lineBrush, StrokeThickness = LineThickness };
            
            Label depth = new Label() { Content = "井\r\n深\r\n(m)", FontSize = 15, Foreground = Brushes.Black };
            Canvas.SetTop(depth, 10);
            Canvas.SetLeft(depth, 20);
            _headerCanvas.Children.Add(depth);

            // 刻度Canvas
            Line left = new Line() { X1 = 0, Y1 = 0, X2 = 0, Y2 = _scaleCanvas.Height, Stroke = lineBrush, StrokeThickness = LineThickness };
            Line right = new Line() { X1 = _scaleCanvas.Width, Y1 = 0, X2 = _scaleCanvas.Width, Y2 = _scaleCanvas.Height, Stroke = lineBrush, StrokeThickness = LineThickness };
            Line bottom = new Line() { X1 = 0, Y1 = _scaleCanvas.Height, X2 = _scaleCanvas.Width, Y2 = _scaleCanvas.Height, Stroke = lineBrush, StrokeThickness = LineThickness };

            _scaleCanvas.Children.Add(left);
            _scaleCanvas.Children.Add(right);
            _scaleCanvas.Children.Add(bottom);            
        }

        private void drawStaticScale()
        {
            int interval = 100;
            for (int i = 0; i * interval < _model.DefaultColumnHeight; ++i)
            {
                TextBlock block = new TextBlock();
                block.Text = (i * _model.Scale * interval + _model.DEPTMEAS.Min).ToString();
                block.Foreground = Brushes.Black;
                block.FontSize = 12;
                Canvas.SetLeft(block, 20);
                Canvas.SetTop(block, i * interval + _model.DefaultHeaderHeight);
                this.Children.Add(block);
            }
        }
        #endregion
        private void addColumnToTheRight(object sender, RoutedEventArgs args)
        { 
        
        }

        private void alterColumn(object sender, RoutedEventArgs args)
        { 
        
        }

        private void saveConfig(object sender, RoutedEventArgs args)
        {

        }
        #region ContextMenu事件处理程序


        #endregion
        #endregion

        #region 动态图表
        #endregion
    }
}
